﻿using MCUPackage;
using MCUPackage.Utils;
using System;
using System.ComponentModel;

namespace SxPackages.STC32G.Models
{
	[Serializable]
	class USART2Options : USARTOptionBase
	{
		public override Boolean Enable
		{
			get => base.Enable;
			set
			{
				if (value != base.Enable)
				{
					if (value)
					{
						if (mParent.SPIs != null && mParent.mSPIs.USART2.Enable)
						{
							GuiUtils.MsgBox("串口2已被用作SPI口使用!");
							return;
						}
					}
					base.Enable = value;
				}
			}
		}

		private USART2Pins mPins = USART2Pins.RxD_P10_TxD_P11;
		[DisplayName("使用引脚"),
		DefaultValue(USART2Pins.RxD_P10_TxD_P11),
		Description("本串口使用的引脚设置")]
		public USART2Pins Pins
		{
			get => mPins;
			set
			{
				if (value != mPins)
				{
					if (SwitchPins(value, mPins))
					{
						mPins = value;
					}
				}
			}
		}

		protected override void SetPins(String[] aPins)
		{
			var opt = mParent.SetPinPurpose(aPins[0], PinPurpose.USART2_RCV);
			opt.Mode = GPIOMode.Standard;
			opt.Speed = GPIOSpeed.Fast;
			opt.EnableUp = true;
			opt = mParent.SetPinPurpose(aPins[1], PinPurpose.USART2_SND);
			opt.Mode = GPIOMode.Push;
			opt.Speed = GPIOSpeed.Fast;
		}

		public USART2Options(STC32GQFP64 aParent) : base(aParent)
		{
			Id = 2;
			mUsePins = TemplateHelper.GetPins(mPins);
		}
	}
}
